package com.itheima.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import java.time.LocalDateTime;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
        //
        log.info("购物车一条数据：{}",shoppingCart);

       //调用自定义方法
        ShoppingCart serviceOne = shoppingCartService.add(shoppingCart);

        //返回购物车对象
        return  R.success(serviceOne);
    }


    /**
     * 修改购物车 菜品或套餐 数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> delete(@RequestBody ShoppingCart shoppingCart){
        log.info("dish_id/setmeal_id为：{}",shoppingCart);

        //调用自定义方法
        ShoppingCart shoppingCart1 = shoppingCartService.delete(shoppingCart);

        return R.success(shoppingCart1);

    }


    /**
     * 查询所有购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //
        log.info("查询所有购物车信息！");

        // 构造条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //  user_id = 当前登录用户id
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        // 时间升序
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        //SQL： select * from shopping_cart where user_id = ? order by createtime asc
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){

        //调用方法
        R<String> clean = shoppingCartService.clean();

        return clean;
    }

}
